import Vue from 'vue'
import Router from 'vue-router'
import Repeat from '@/layout/repeat'
import Layout from '@/layout/index'

Vue.use(Router)

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/refresh',
    component: () => import('@/views/refresh'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }
]

/**
 * 菜单数组转菜单路由
 * @param arr
 * @param pid
 * @returns {[]}
 */
export const arrayToRouter = (arr, pid = 0) => {
  const menus = []
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].pid === pid) {
      const item = {
        name: arr[i].menu_name,
        path: delPrefix(arr[i].uri),
        hidden: arr[i].is_hide === 1,
        meta: {
          id: arr[i].menu_id,
          title: arr[i].menu_name,
          pid: arr[i].pid,
          icon: arr[i].icon,
          auth: null
        }
      }
      const component = components[item.path] // 获取视图组件
      if (component) {
        item.component = component.component || null
        if (component.uri) {
          item.path = component.uri
        }
        if (component.hidden === true) {
          item.hidden = true
        }
        if (component.name) {
          item.name = component.name
        }
        item.meta.auth = component.auth ? component.auth : null
      } else {
        item.component = null
      }
      const children = arrayToRouter(arr, item.meta.id)
      if (children.length) {
        if (!item.redirect && !item.path) {
          item.path = item.redirect = children[0].redirect || children[0].path
        } else if (item.path) {
          item.redirect = item.path
        } else if (item.redirect && !item.path) {
          item.path = this.redirect
        }
        item.component = Repeat
        item.children = children
      }
      menus.push(item)
    }
  }
  return menus
}

/**
 * 附加默认路由
 * @param menus
 * @returns {*[]}
 */
export const routers = (menus) => {
  return [
    {
      path: '/',
      hidden: true,
      children: menus,
      component: Layout,
      redirect: menus[0].redirect || menus[0].path
    },
    { path: '*', redirect: '/404', hidden: true }
  ]
}

/**
 * 删除前缀
 * @param uri
 * @returns mixins
 */
const delPrefix = (uri) => {
  return uri ? uri.replace(process.env.VUE_APP_PREFIX, '') : null
}

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const components = {
  // 系统管理
  '/user/paginate': { component: resolve => require(['@/views/user'], resolve) },
  '/print/tpl/paginate': { component: resolve => require(['@/views/print-tpl'], resolve) },
  '/menu/paginate': { component: resolve => require(['@/views/menu'], resolve) },
  '/group/paginate': { component: resolve => require(['@/views/group'], resolve) },
  '/config/wechat/gzh': { component: resolve => require(['@/views/config/gzh'], resolve) },
  '/config': { component: resolve => require(['@/views/config'], resolve) },

  // 数据管理
  '/user/manage': { component: resolve => require(['@/views/user/manage'], resolve) },
  '/user/importedit': { component: resolve => require(['@/views/user/importEdit'], resolve) },
  '/goods/paginate': { component: resolve => require(['@/views/goods'], resolve) },
  '/goods/import': { component: resolve => require(['@/views/goods/import'], resolve) },
  '/goods/importedit': { component: resolve => require(['@/views/goods/importEdit'], resolve) },
  '/bid/paginate': { component: resolve => require(['@/views/bid'], resolve) },
  '/bid/goods': { component: resolve => require(['@/views/bid/goods'], resolve) },
  '/bid/summary': { component: resolve => require(['@/views/bid/summary'], resolve) },
  '/supplier/paginate': { component: resolve => require(['@/views/supplier'], resolve) }
}

export default router
